#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList2.h"
void SLInit(SL* ps)
{
	ps->data = NULL;
	ps->capacity = ps->size = 0;
}
void SLDestory(SL* ps)
{
	if (ps->data != NULL)
	{
		free(ps->data);
		ps->data = NULL;
	}
	ps->capacity = ps->size = 0;
}
//void SLPrint(SL* ps)
//{
//	for (int i = 0; i < ps->size; i++)
//	{
//		printf("%d ", ps->data[i]);
//	}
//	printf("\n");
//}
void SLCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->data, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		else
		{
			ps->data = tmp;
			ps->capacity = newcapacity;
		}
	}
}
void SLPushBack(SL* ps, SLDataType x)
{
	SLInest(ps, ps->size, x);
}

void SLPushFront(SL* ps, SLDataType x)
{
	SLInest(ps, 0, x);
}
void SLPopBack(SL* ps)
{
	assert(ps->size > 0);
	SLErase(ps, ps->size-1);
}
void SLPopFront(SL* ps)
{
	assert(ps->size > 0);
	SLErase(ps, 0);
}

void SLInest(SL* ps, int pos, SLDataType x)
{
	assert(pos <= ps->size);
	SLCheckCapacity(ps);
	int end = ps->size - 1;
	for (end; end >= pos; end--)
	{
		ps->data[end + 1] = ps->data[end];
	}
	ps->data[pos] = x;
	ps->size++;
}

void SLErase(SL* ps, int pos)
{
	assert(pos < ps->size);
	int start = pos;
	for (start; start < ps->size; start++)
	{
		ps->data[start] = ps->data[start + 1];
	}
	ps->size--;
}

//int SLFind(SL* ps, SLDataType x)
//{
//	for (int i = 0; i < ps->size; i++)
//	{
//		if (ps->data[i] == x)
//		{
//			return i;
//		}
//	}
//	return -1;
//}